#!/usr/bin/env bash

# 中文环境 zh_CN.UTF-8
export LANG=zh_CN.UTF-8

# bash 版本 4 及以上
if (( ${BASH_VERSINFO[0]} < 4 )); then
    echo "Bash version is too low, please use Bash 4 or later." >&2
    exit 1
fi

# 进入根目录
[[ $0 =~ (.*)/ ]] && cd ${BASH_REMATCH[1]}/..
# 记录根目录路径
export RootDir=${PWD:-$(pwd)}
# 返回之前的目录
cd - &>/dev/null
# 设置只读
declare -r RootDir

# 设置其他目录的环境变量
export CoreDir="${RootDir}/core"
declare -r CoreDir

export UsrDir="${RootDir}/usr"
declare -r UsrDir

export LibDir="${CoreDir}/library"
declare -r LibDir

export ModuleDir="${CoreDir}/module"
declare -r ModuleDir

export TmpDir="${RootDir}/.TmpDir"
declare -r TmpDir

# 默认运行的控制台函数
default_console_run()
{
    # 读取所有命令的名字
    IFS=$'\n' read -d "" -ra cmd_names <<< "$(command_name_travel)"
    # 创建tab补全集合
    cmd_tabmark="$(create_cmd_tab ${cmd_names[*]})"
    while true; do
        # 读取用户命令
        user_cmd="$(cmd_read --tab $cmd_tabmark)"
        # 执行
        [[ -z $user_cmd ]] || DO_CMD $user_cmd
    done
    # 删除tab补全集合
    delete_cmd_tab "$cmd_tabmark"
}

# 控制台函数，整个系统最终运行的函数。
console_run()
{
    default_console_run
    return
}
# 参数集合
_MAIN_ARGS__=()
# 检查设置参数
check_setting()
{
    # default setting
    # default enable tree and nothing level, tree echo enable or disable, Debug level: nothing, message, info
    tree_echo_enable && DEBUG_LEVEL="nothing"

    #user setting
    for option in $*; do
        [[ $option == "--debug" ]] && DEBUG_LEVEL="info" && continue
        [[ $option == "--info-debug" ]] && DEBUG_LEVEL="info" && continue
        [[ $option == "--message-debug" ]] && DEBUG_LEVEL="message" && continue
        [[ $option == "--nodebug" ]] && DEBUG_LEVEL="nothing" && continue
        [[ $option == "--tree" ]] && tree_echo_enable && continue
        [[ $option == "--notree" ]] && tree_echo_disable && continue
        _MAIN_ARGS__+=( "$option" )
    done
}

# 退出
_exit__()
{
    modules_exit
    [[ -d ${TmpDir} ]] && rm -rf ${TmpDir}
    debug_echo "ByeBye~"
}

# 初始化
_init__()
{
    check_setting $*
    debug_echo "Loading base modules..."
    module_load "${ModuleDir}/print.sh"
    module_load "${ModuleDir}/configure.sh"
    module_load "${ModuleDir}/command.sh"
    module_load "${ModuleDir}/history.sh"
    module_load "${ModuleDir}/read.sh"
    module_load "${ModuleDir}/menus.sh"
    module_load "${UsrDir}/usr.sh"
    debug_echo "A total of ${LOAD_MODULE_COUNT} base modules were loaded in ${SECONDS} seconds."
    debug_echo ""
}

# __MAIN___
# 创建临时文件目录
[[ -d ${TmpDir} ]] || mkdir ${TmpDir}
# 加载必要的库函数
source ${LibDir}/debug.sh
source ${LibDir}/system.sh
source ${LibDir}/file.sh
source ${LibDir}/string.sh
source ${LibDir}/array.sh
source ${LibDir}/module.sh

# 设置退出函数
trap '_exit__' EXIT
# 初始化
_init__ $*
# 运行
if [[ "${_MAIN_ARGS__[*]}" == "-p" ]]; then
    # 只通过命令行运行
    default_console_run
elif [[ -t 1 && -p /dev/stdin ]]; then
    # 通过终端管道运行
    DO_CMD_BY_FILE "/dev/stdin"
elif arg="$(find_arg "-f" "${_MAIN_ARGS__[*]}")"; then
    # 通过文件运行
    DO_CMD_BY_FILE "$arg"
elif (( ${#_MAIN_ARGS__[@]} > 0 )); then 
    # 执行一条命令
    DO_CMD ${_MAIN_ARGS__[*]}
else
    # 非上述情况，显示菜单
    console_run
fi 

